$(document).ready(function() {
	var contextPath = document.location.pathname;
	var index = contextPath.substr(1).indexOf("/");
	contextPath = contextPath.substr(1, index);
	var basePath = "/" + contextPath;
	var giftId= $(".color").attr("id");
	var initURL =basePath+"/HRTWEB/findAll.do";
						 var radios = document.getElementsByName("color");
						 //getSelect(radios,giftId);
						 for (var i = 0; i < radios.length; i++) {
						        radios[i].indexs = i + 1;
						        radios[i].onchange = function () {
						            if (this.checked) {
						            	
						            	var colorName= $(this).attr("colorName");
						            	var queryUrl =encodeURI(basePath+"/HRTWEB/find.do?giftId="+giftId+"&color="+colorName);
						            	$.ajax({
											url : queryUrl,
											dataType : 'json',
											cache : false,
											success : function(data) {
												clearItemTable();
												var result = data.gdDetail;
												var measures = $("#measures");
												for(i=0;i<result.length;i++){
													var rowContent = "<span><input type='radio'  class='gdName' name='gdName' value='"+result[i].gdName+"'>"+result[i].gdName+"</input><span>";
													measures.append(rowContent);
												}
											},
											error : function(html) {
												alert("出错了");
												return;
											}
										});
						            }
						        }
						    }
						 
						 $.ajax({
								url : initURL,
								dataType : 'json',
								data:{
									giftId:giftId
								},
						 
								cache : false,
								success : function(data) {
									clearItemTable();
									var result = data.gdDetail;
									var measures = $("#measures");
									for(i=0;i<result.length;i++){
										var rowContent = "<span><input type='radio'  class='gdName' name='gdName' gdName='"+result[i].gdName+"' value='"+result[i].gdName+"'>"+result[i].gdName+"</input><span>";
										measures.append(rowContent);
									}
									  gdRadios = document.getElementsByName("gdName");
									  getSelect(gdRadios,giftId,basePath);
									
								},
								error : function(html) {
									alert("出错了");
									return;
								}
							});
						 $("button[name='buyGift']").bind("click",function(){
							 $("#shopType").attr("value",1);
							 $("#orderInfor").submit();
						 });
						 $("button[name='addCar']").bind("click",function(){
							 $("#shopType").attr("value",2);
							 $("#orderInfor").submit();
						 });
						/* $("input[name='color']").bind("click",function(){ 
							 var color= $(this).val();
							 alert(color); 
							 
						 }); 
						 $("input[name='gdName']").bind("click",function(){ 
							 
							 alert($(this).val()); 
							 
						 }); */
});
function clearItemTable() {

	$("#measures span").remove();
}
function clearColorList() {
	
	$("#colorList span").remove();
}

function getSelect(gdRadios,giftId,basePath){
	for (var i = 0; i < gdRadios.length; i++) {
	    gdRadios[i].indexs = i + 1;
	    gdRadios[i].onchange = function () {
	        if (this.checked) {
	        	var gdName= $(this).attr("gdName");
	        	var colorName= $(this).attr("colorName");
	        	alert("gdName----------1"+gdName);
	        	var queryUrl;
	        	if(gdName==null||gdName=='undefined'){
	        		alert("gdName------------"+gdName);
	        		
	        		queryUrl =encodeURI(basePath+"/HRTWEB/find.do?giftId="+giftId+"&color="+colorName);
	        	}else{
	        		alert("colorName------------"+colorName);
	        		queryUrl =encodeURI(basePath+"/HRTWEB/findByGdName.do?giftId="+giftId+"&gdName="+gdName);
	        		alert("queryUrl------------"+queryUrl);
	        	}
	            $.ajax({
	                url : queryUrl,
	                dataType : 'json',
	                cache : false,
	                success : function(data) {
	                	clearColorList();
	                    var result = data.detail;
	                    var colorList = $("#colorList");
	                   
	                        var rowContent = "<span><input type='radio'  class='color' name='color' value='"+result.color+"'>"+result.color+"</input><span>";
	                        colorList.append(rowContent);
	                   
	                },
	                error : function(html) {
	                    alert("出错了");
	                    return;
	                }
	            });
	        }
	    }
	}
}
